<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<%@ include file="/common/plateform/include/common-base.jsp"%>
	<title>数据源配置</title>
	<!-- Tell the browser to be responsive to screen width -->
	<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
</head>
<body class="hold-transition ${sysColor} sidebar-mini">
<div class="wrapper">

	<!-- top start -->
	<%@include file="/common/plateform/top.jsp" %>
	<!-- top end -->
	<!-- left start -->
	<%@include file="/common/plateform/left-project.jsp" %>
	<!-- left end-->

	<div class="content-wrapper">
		<!-- Main start -->
		<section class="content">
			<div class="row">
				<div class="col-xs-12">

					<div class="box box-info">
						<div class="box-header with-border">
							<h3 class="box-title"><i class="fa fa-cog"></i> 数据源配置</h3>
						</div>
						<div class="box-body">
							<form id="sourceForm" class="form-horizontal" action="javascript:;">
								<input type="hidden" name="pageName" value="datasource-list">
								<input type="hidden" name="formName" value="editForm">
								<input type="hidden" name="tRpcDatasource.sysEname" data-name="tRpcDatasource.sysEname"/>
								<input type="hidden" id="input_data_name" name="tRpcDatasource.w_dataName" data-name="tRpcDatasource.dataName"/>
								<table class="table">
									<tbody>
										<tr>
											<td class="text-right w200"><label class="form-control-static">数据源名称：</label></td>
											<td>
												<div class="form-group form-control-static" id="span_dataName_text" data-name="tRpcDatasource.dataName"></div>
											</td>
										</tr>
										<tr>
											<td class="text-right"><label class="form-control-static">数据源类型：</label></td>
											<td>
												<div class="form-group form-control-static isview" data-name="tRpcDatasource.databaseType"></div>
												<div class="form-group w400 isedit hidden">
													<select class="form-control" name="tRpcDatasource.databaseType" data-name="tRpcDatasource.databaseType" onchange="changeDataType(this)">
														<option value="mysql" selected="selected">mysql（推荐）</option>
														<option value="oracle">oracle</option>
														<!--
                                                        <option value="sqlserver">sqlserver</option>-->
													</select>
												</div>
											</td>
										</tr>
										<tr>
											<td class="text-right"><label class="form-control-static">JDBC Driver class：</label></td>
											<td>
												<div class="form-group form-control-static isview" data-name="tRpcDatasource.driverClassName"></div>
												<div class="form-group w400 isedit hidden">
													<select class="form-control" name="tRpcDatasource.driverClassName"  data-name="tRpcDatasource.driverClassName">
														<option data-type="mysql" value="com.mysql.jdbc.Driver" selected="selected">com.mysql.jdbc.Driver</option>
														<option data-type="oracle" value="oracle.jdbc.OracleDriver">oracle.jdbc.OracleDriver</option>
														<!--<option data-type="sqlserver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver">com.microsoft.sqlserver.jdbc.SQLServerDriver</option>-->
													</select>
												</div>
											</td>
										</tr>
										<tr>
											<td class="text-right"><label class="form-control-static">Database URL：</label></td>
											<td style="word-break:break-all; word-wrap:break-word;">
												<div class="form-group form-control-static isview" data-name="tRpcDatasource.jdbcUrl"></div>
												<div class="form-group isedit hidden">
													<input name="tRpcDatasource.jdbcUrl" class="form-control clean" data-name="tRpcDatasource.jdbcUrl" placeholder="此项不能为空,参考下面格式"/>
													<div class="f12 fx8">
														<div>[mysql]：jdbc:mysql://127.0.0.1:3307/test?useUnicode=true&characterEncoding=UTF8&allowMultiQueries=true&useSSL=false</div>
														<div>[oracle]：jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl</div>
														<!--<div>[sqlserver]：jdbc:sqlserver://127.0.0.1:1433;DatabaseName=testdb</div>-->
													</div>
												</div>
											</td>
										</tr>
										<tr>
											<td class="text-right"><label class="form-control-static">UserName：</label></td>
											<td>
												<div class="form-group w400 isedit hidden">
													<input name="tRpcDatasource.jdbcUsername" class="form-control clean" data-name="tRpcDatasource.jdbcUsername" value="ius_base" placeholder="此项不能为空"/>
												</div>
												<div class="form-group form-control-static isview" data-name="tRpcDatasource.jdbcUsername" data-format="showLast"></div>
											</td>
										</tr>
										<tr>
											<td class="text-right"><label class="form-control-static">Password：</label></td>
											<td>
												<div class="form-group w400 isedit hidden">
													<input name="tRpcDatasource.jdbcPassword" class="form-control clean" data-name="tRpcDatasource.jdbcPassword" value="ius_base" placeholder="此项不能为空"/>
												</div>
												<div class="form-group form-control-static isview" data-name="tRpcDatasource.jdbcPassword" data-format="showLast"></div>
											</td>
										</tr>
										<tr>
											<td class="text-right"><label class="form-control-static">是否启用编辑：</label></td>
											<td>
												<div class="form-group w400">
													<input id="input_checkbox_edit_switch" type="checkbox" checked class="checkbox" value=""/>
												</div>
											</td>
										</tr>
										<tr id="tr_btnOpt" class="hidden">
											<td colspan="2" class="text-center">
												<button type="button" class="btn btn-danger" onclick="saveItem('#sourceForm');">提交保存</button>
												<button id="btn-load-cache" type="button" class="btn btn-primary" onclick="loadCache();"><i class="fa fa-refresh"></i> 刷新缓存 </button>
											</td>
										</tr>
									</tbody>
								</table>
							</form>
						</div>
					</div>
				</div>
			</div>
		</section>
		<!-- Main end -->

	<script>
		var clickMenu = "datasource-list";
		var formId = "#sourceForm";
		var defSysEname =  strUtil.doNull(strUtil.getLocalStorage("sysEname"), "");

		$(function () {
		    //开关切换
            $("#input_checkbox_edit_switch").bootstrapSwitch({
                onText:"开启",
				offText:"关闭",
                onColor:"primary",
                offColor:"warning",
                size : "mini",
				state : false,
                handleWidth:"35",//设置控件宽度
                // 当开关状态改变时触发
                onSwitchChange : function(event, state) {
                    console.log("state",state);
                    if(state){
                        $(formId).find(".isedit").removeClass("hidden");
                        $(formId).find(".isview").addClass("hidden");
                        $("#tr_btnOpt").removeClass("hidden");
					}else{
                        $(formId).find(".isedit").addClass("hidden");
                        $(formId).find(".isview").removeClass("hidden");
                        $("#tr_btnOpt").addClass("hidden");
					}
				}
            });
            //加载数据
            mytools.cleanData(formId);
            toLoadInfo();
		});

		//读取（查询、编辑）数据
		function toLoadInfo(){
            var index_load = layer.load(1, {shade: false});
            var dataName = defSysEname;
            if(defSysEname=='plateform'){
                dataName = "default";
			}
			$.ajax({ type:'get', url:common_list_url,dataType:'json',
				data:{'pageName':'datasource-list','formName':'searchForm','tRpcDatasource.w_dataName':dataName},
				success: function(myData){
                    layer.close(index_load);
                    console.log("tRpcDatasource",myData);
                    if(myData.total>0){
                        $(formId).find("input[name='formName']").val("editForm");
                        $(formId).find("#input_data_name").attr("name","tRpcDatasource.w_dataName");
                        FormUtil.loadView("tRpcDatasource", myData.rows[0]["tRpcDatasource"]);
                        $("#btn-load-cache").removeClass("hidden");
                    }else{
                        var selectDataBaseTypeObj =$(formId).find("select[name='tRpcDatasource.databaseType']");
                        selectDataBaseTypeObj.val("${database_type}");//设置指定数据源类型选中，同时关联控制下面的驱动下拉框
                        $(formId).find("select[name='tRpcDatasource.driverClassName']").get(0).selectedIndex=selectDataBaseTypeObj.prop('selectedIndex');

                        $(formId).find("input[name='formName']").val("addForm");
                        $(formId).find("#input_data_name").attr("name","tRpcDatasource.dataName").val(defSysEname);
                        $(formId).find("#span_dataName_text").text(defSysEname);
                        $(formId).find("input[name='tRpcDatasource.sysEname']").val(defSysEname);
                        $("#input_checkbox_edit_switch").bootstrapSwitch('toggleState',true);
                        $(formId).find(".isedit").removeClass("hidden");
                        $(formId).find(".isview").addClass("hidden");
                        $("#tr_btnOpt").removeClass("hidden");
                        $("#btn-load-cache").addClass("hidden");
					}
				}
			});
		}

        //切换数据库类型，对应所需的驱动类
        function changeDataType(obj){
            //添加【数据库类型】切换事件
            var index =$(obj).prop('selectedIndex');
            $(formId).find("select[name='tRpcDatasource.driverClassName']").get(0).selectedIndex=index;
        }

		//提交数据
		function saveItem(formId){
            var jdbcUrl_Obj = $(formId).find("input[name='tRpcDatasource.jdbcUrl']");
            if (strUtil.isNull(jdbcUrl_Obj.val())) {
                layer.tips("此项不能为空！",jdbcUrl_Obj,{tips:3});
                jdbcUrl_Obj.focus();
                return;
            }
            var jdbcUsername_Obj = $(formId).find("input[name='tRpcDatasource.jdbcUsername']");
            if (strUtil.isNull(jdbcUsername_Obj.val())) {
                layer.tips("此项不能为空！",jdbcUsername_Obj,{tips:3});
                jdbcUsername_Obj.focus();
                return;
            }
            var jdbcPassword_Obj = $(formId).find("input[name='tRpcDatasource.jdbcPassword']");
            if (strUtil.isNull(jdbcPassword_Obj.val())) {
                layer.tips("此项不能为空！",jdbcPassword_Obj,{tips:3});
                jdbcPassword_Obj.focus();
                return;
            }
			var index_submit = layer.confirm('确认提交保存操作吗？', {btn: ['确认','取消'],closeBtn: 0,offset: '220px'}
			, function() {
					layer.close(index_submit);
                    var index_load = layer.load(1, {shade: false});
					$.ajax({ type:'post', url:common_submit_url,dataType:'json',
						data:$(formId).serialize(),
						success: function(myData){
                            layer.close(index_load);
							console.log("add.tRpcDatasource",myData);
							//layer.alert(myData.msg, {icon: 1,closeBtn: 0,offset: '220px'});
							if(myData.flag=='T'){
							    toLoadInfo();
                                loadCache();
								layer.alert("操作成功");
							}else{
								layer.alert(myData.msg);
							}
						}
					});
			});
		}
		//刷新缓存
		function loadCache(){
            $("#input_checkbox_edit_switch").bootstrapSwitch('toggleState',false);
            mytools.loadCacheDatasource(defSysEname);
        }

	</script>

	</div>
	<!-- /.content-wrapper -->
	<!-- foot start -->
	<%@include file="/common/plateform/foot.jsp" %>
	<!-- foot start -->


</body>



</html>
